<?php 
@session_start();
require_once("../connect_db.php");
$sqlCourse="select course.course_id,course_name,hour_num,order_id,order_status from course,orders  where course.course_id=orders.course_id and std_id='".$_SESSION["id"]."' ";
$queryCourse=mysql_query($sqlCourse);

?>

<form method="post" enctype="multipart/form-data" name="frmreg" id="frmreg">

<div class="row">
	<div class="col-md-12">
		<div class="panel">
		 
		  <div class="panel-body">
		 
		  
			<table cellpadding="0" cellspacing="0" border="0" class="display" id="Course" width="100%" style="font-size:14px">
	<thead>
		<tr>
		
			<th style="width:30%;text-align: center !important;color:black; !important;font-size:1em">ชื่อหลักสูคร</th>
			<th style="width:20%;text-align: center !important;color:black; !important;font-size:1em">รายละเอียด</th>
			<th style="width:20%;text-align: center !important;color:black; !important;font-size:1em">เวลาคงเหลือ(ชม.)</th>
			<th style="width:30%;text-align: center !important;color:black; !important;font-size:1em">ลงทะเบียน</th>
			
		</tr>
	</thead>
	<tbody style="color:black !important;">
	
		<?php 
		$countRegis_course=0;
		while($resultCourse=mysql_fetch_array($queryCourse)){
		
		
		$countRegis_course+=1;
		$sqlHour="select sum(meet_hour) as sumH from  orders  O LEFT JOIN meeting M ON O.order_id=M.order_id where  std_id='".$_SESSION["id"]."' and order_status='P' and O.course_id='".$resultCourse["course_id"]."' group by course_id ";
		$queryHour=mysql_query($sqlHour);
		$resultHour=mysql_fetch_array($queryHour);
		if(!empty($resultHour["sumH"])||$resultHour["sumH"]>0){
		$sumH=$resultCourse["hour_num"]-$resultHour["sumH"];
		}
		else{
		$sumH=$resultCourse["hour_num"];}
		
		
		$sqlMeet="select count(meet_id) as countMeet,meet_datetime,meet_status from meeting,orders where meeting.order_id=orders.order_id";
		$sqlMeet.=" and  meet_status='N' and meeting.order_id='".$resultCourse["order_id"]."' ";
		$queryMeet=mysql_query($sqlMeet);
		$resultMeet=mysql_fetch_array($queryMeet);
		
		$sqlMeet2="select count(meet_id) as countMeet,meet_datetime,meet_status from meeting,orders where meeting.order_id=orders.order_id";
		$sqlMeet2.=" and  meet_status='N2' and meeting.order_id='".$resultCourse["order_id"]."' ";
		$queryMeet2=mysql_query($sqlMeet2);
		$resultMeet2=mysql_fetch_array($queryMeet2);
		
	
		?>
		<tr>
			<td style="text-align: center !important;color:black; !important;font-size:1em"><?php echo $resultCourse["course_name"]?></td>
			<td style="text-align: center !important;color:black; !important;font-size:1em">
			<img class="popup_window pics" temp="<?php echo $resultCourse["course_id"]?>"  src="../pic/info.png" width="50" />
			</td>
			<td style="text-align: center !important;color:black; !important;font-size:1em"><?php echo $sumH?></td>
			<td style="text-align: center !important;color:black; !important;font-size:1em">
			<?php 
			
			switch($resultCourse["order_status"]){
			case "WP": 
				echo '<button class="btn btn-danger" onClick="return false;">กำลังตรวจสอบการโอนเงิน...</button>';
			break;
			case "W": 
				echo '<button class="btn btn-danger nav_link2" txt="กรอกฟอร์มการแจ้งการลงทะเบียน" url="student/payment_course.php?temp='.$resultCourse["order_id"].'">แจ้งการโอนเงิน...</button>';
			break;
			case 'P':
			if($resultMeet["countMeet"]==0&&$resultMeet2["countMeet"]==0&&isset($resultCourse["order_id"])){
				echo '<button class="btn btn-default nav_link2" txt="กรุณาเลือกวันที่ต้องการจะนัดเรียน" url="student/meet_course.php?temp='.$resultCourse["order_id"].'">ลงเวลาเรียน...</button>';
			}
			else{
			if($resultMeet["meet_datetime"]!=""){
			$cdate=date('Y-m-d H:i:s', strtotime($resultMeet["meet_datetime"]. ' - 2 day'));
			$date=substr($resultMeet["meet_datetime"],0,10);}else{
			$cdate=date('Y-m-d H:i:s', strtotime($resultMeet2["meet_datetime"]. ' - 2 day'));
			$date=substr($resultMeet2["meet_datetime"],0,10);}
			if(date('Y-m-d H:i:s')<$cdate)
			{
			if($resultMeet2["meet_status"]=='N2'){
			echo '<b>ลงเวลาเรียนแล้ว...</b>';
			}else{
			echo '<button class="btn btn-warning nav_link2" txt="แก้ไขนัดเรียน" url="student/meet_course.php?edit=1&temp='.$resultCourse["order_id"].'">แก้ไขเวลาเรียน...</button>';}
			}
			else if(date('Y-m-d')==$date){
			echo '<b style="color:green;">ถึงวันที่นัดเรียน...</b>';
			}
			else{
			echo '<b style="color:orange;">ลงเวลาเรียนแล้ว...</b>';
			}
			}
			break;			
			
			 }
			?>
			</td>
			
		</tr>
		<?php }?>
	</tbody>
	
</table>
		  </div>
		</div>
	</div>
</div>
</div>

 
<div id="data-popup" class="popup">
    <div class="popup-body"><span class="popup-exit"></span>
 
        <div class="popup-content">
            <h2 class="popup-title">ข้อมูลหลักสูตร</h2>
			<div class="popup-des">
			<img class="pics" src="../pic/bx_loader.gif" width="50" />
			</div>
        </div>
    </div>
</div>
<div class="popup-overlay"></div>
</form>
<script>
$(document).ready(function(){
$(".nav_link2").click(function(e){
				$("#h3title").html($(this).attr("txt"));
				$("#mainscreen").load($(this).attr("url"));
				return false;
			});
  	oTable = $('#Course').dataTable({
          "bJQueryUI": true,
          "sPaginationType": "full_numbers"
      });

$('.popup_window').click(function () {
        $('html').addClass('overlay');
        var activePopup = "#data-popup";
        $(activePopup).addClass('visible');
		 $('.popup-overlay').click(function () {
        clearPopup();
		});
		$.ajax({
		url:"admin/preview_course_data.php?temp="+$(this).attr("temp"),
			success:function(res){
			$(".popup-des").html(res);
			 $('.popup-exit').click(function () {
				clearPopup();
				});
			}
		});
 
    });
 
    $(document).keyup(function (e) {
        if (e.keyCode == 27 && $('html').hasClass('overlay')) {
            clearPopup();
        }
    });
 
      
 
});
	  
	
	 function clearPopup() {
        $('.popup.visible').addClass('transitioning').removeClass('visible');
        $('html').removeClass('overlay');
 
        setTimeout(function () {
            $('.popup').removeClass('transitioning');
        }, 200);
    }
</script>